﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Quizz.Library.Helper;
using System.Web;
using Quizz.Infrastructure;

namespace Quizz.Library.Filter
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    public class EditorFilter : AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (AccessFactory.IsEditorLogined()) return;
            if (IsAjax)
            {
                filterContext.Result = new HttpUnauthorizedResult();
                filterContext.Result = new JsonNetResult
                                           {
                                               ContentEncoding = Encoding.UTF8,
                                               JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                                               Data = new
                                                          {
                                                              result = false,
                                                              message = "Bạn không có quyền truy cập. Vui lòng đăng nhập trước."
                                                          }
                                           };
            }
            else
            {
                filterContext.Result = Constants.HomePage;
            }
        }

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            return AccessFactory.IsUserLogined();
        }

        public EditorFilter()
        {
            IsAjax = false;
        }

        public bool IsAjax { get; set; }
    }
}
